package asteroids.model.programs.parsing.statements;

import java.util.List;
import java.util.Map;

import be.kuleuven.cs.som.annotate.Basic;

import asteroids.model.Ship;
import asteroids.model.programs.parsing.expressions.Expression;
import asteroids.model.programs.parsing.types.Type;

/**
 * A class of assignments.
 * 
 * @author Thijs Dieltjens - Wim Konings
 * @version 2.0
 */
public class Assignment extends Statement {

	public Assignment(String x, Expression e,int line, int column){
		super(line, column);
		this.variable = x;
		this.expression = e;
	}

	@Basic
	public Expression getExpression() {
		return expression;
	}

	private Expression expression;

	@Basic
	public String getVariable(){
		return variable;
	}

	private String variable;

	public String toString(){
		return getVariable()+" :"+getExpression().toString();
	}

	@Override
	public void execute(Ship ship) {
		ship.getProgram().addGlobalVariable(getVariable(), getExpression().eval(ship, ship.getProgram().getGlobals()));
	}

	@Override
	public List<String> getTypeCheckErrors(List<String> errors, Map<String, Type> globals) {
		if(globals.isEmpty() || !globals.containsKey(getVariable()) || !(getExpression().eval(null, globals).getClass()==globals.get(getVariable()).getClass())){
			errors.add(getLine()+"-"+getColumn()+": Assignment is not valid.");
		}
		errors = getExpression().getTypeCheckErrors(errors, globals);
		return errors;
	}

	@Override
	public boolean containsActionStatement() {
		return false;
	}
}
